<?php

namespace catchAdmin\drug\controller;

use catchAdmin\drug\service\MedicationReminderService;
use catcher\CatchResponse;
use catcher\base\CatchRequest;
use catcher\base\CatchController;
use app\common\model\MedicationReminder as ReminderModel;
use catchAdmin\drug\request\MedicationReminderRequest;
use catcher\exceptions\FailedException;
use MongoDB\BSON\ObjectId;

class MedicationReminder extends CatchController
{
    protected $reminder;

    public function __construct(ReminderModel $reminder)
    {
        $this->reminder = $reminder;
    }

    /**
     * 列表
     * @time 2023年08月17日
     * @param CatchRequest $request
     * @return \think\response\Json
     */
    public function index(CatchRequest $request): \think\response\Json
    {
        $req = $request->param();
        $where = [];
        if (isset($req['patientId']) && !empty($req['patientId'])) {
            $where[] = ['patientId', '=', $req['patientId']];
        }
        return CatchResponse::paginate($this->reminder->getList($where));
    }

    /**
     * 新增
     * @param MedicationReminderRequest $request
     * @return \think\response\Json
     */
    public function save(MedicationReminderRequest $request): \think\response\Json
    {
        $req = $request->post();
        $list = (new MedicationReminderService())->saveMedicationReminder($req);
        return CatchResponse::success($list);
    }

    /**
     * 更新
     * @param CatchRequest $request
     * @return \think\response\Json
     */
    public function update($id, CatchRequest $request): \think\response\Json
    {
        $req = $request->post();
        if (isset($req['take_time'])) {
            foreach ($req['take_time'] as $key => $val) {
                if (!isset($val['take_time_id']) && !empty($val['take_time_value'])) {
                    $req['take_time'][$key]['take_time_id'] = (string)new ObjectId();
                    $req['take_time'][$key]['take_time_value'] = $val['take_time_value'];
                }
            }
        }
        return CatchResponse::success($this->reminder->saveData($id, $req));
    }

    /**
     * 删除
     * @param string $id
     * @return \think\response\Json
     */
    public function delete(string $id): \think\response\Json
    {
        return CatchResponse::success($this->reminder->destroy($id));
    }

    /**
     * 用药提醒单位
     * @return \think\response\Json
     */
    public function getReminderUnit(): \think\response\Json
    {
        $cycle = config('dictionary.medication_cycle');
        $frequency = config('dictionary.reminder_frequency');
        $ret = [
            'cycle' => $cycle,
            'frequency' => $frequency
        ];
        return CatchResponse::success($ret);
    }

    /**
     * 获取最新一次用药记录
     * @param CatchRequest $request
     * @return \think\response\Json
     */
    public function getLatestMar(CatchRequest $request): \think\response\Json
    {
        $req = $request->param();

        if (!isset($req['patientId']) || empty($req['patientId']))
        {
            throw new FailedException("患者id不能为空");
        }
        $ret = $this->reminder->where('patientId', $req['patientId'])->order('_id', 'desc')->find();
        return CatchResponse::success($ret);
    }
}